package pl.nazaweb.imedproplus.model.facade;

import java.math.BigInteger;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import pl.nazaweb.imedproplus.model.entity.OrganizationStructure;

/**
 *
 * @author naza
 */
@Stateless
@LocalBean
public class OrganizationStructureFacade extends AbstractFacade<OrganizationStructure> {

    @PersistenceContext(unitName = AbstractFacade.peristanceUnitName)
    private EntityManager entityManager;

    public OrganizationStructureFacade() {
        super(OrganizationStructure.class);
    }

    @Override
    protected EntityManager getEntityManager() {
        return this.entityManager;
    }

    @Override
    public void remove(OrganizationStructure entity) {
        this.getEntityManager()
                .createNativeQuery("delete from organization_structure where id="+entity.getId())
                .executeUpdate();
    }

    @Override
    public void remove(List<OrganizationStructure> entityList) {
        for(OrganizationStructure org : entityList){
            this.remove(org);
        }
    }
    

    public OrganizationStructure getRootOrganizationStructure() {
        return (OrganizationStructure) this.getEntityManager()
                .createNamedQuery("OrganizationStructure.findById")
                .setParameter("id", new Long(-1)).getSingleResult();
    }

    public List<OrganizationStructure> getOrganizationStructureParentChilds(BigInteger parentId) {
        return this.getEntityManager()
                .createNamedQuery("OrganizationStructure.findByParent")
                .setParameter("parent", parentId)
                .getResultList();
    }
    
    public boolean isOrganizationStrucutreInTree(Long id){
        return this.getEntityManager()
                .createNamedQuery("OrganizationStructure.findByIdAndParentIsNull")
                .setParameter("id", id).getResultList().size() > 0 ? false : true;
    }
    
    public void addOrganizationStructureToStructureTree(BigInteger parentId, Long childId){
        this.getEntityManager()
                .createNativeQuery("update organization_structure set parent=:parentId where id=:id")
                .setParameter("parentId", parentId)
                .setParameter("id", childId)
                .executeUpdate();
        this.getEntityManager()
                .createNativeQuery("update organization_structure set has_child=true where id=:id")
                .setParameter("id", parentId.longValue())
                .executeUpdate();
    }
    
    public void removeOrganizationStructureFromTree(Long id, BigInteger parentId){
        this.getEntityManager()
                .createNativeQuery("update organization_structure set parent=null where id=:id")
                .setParameter("id", id)
                .executeUpdate();
        int childsCount = this.getOrganizationStructureParentChilds(parentId).size();
        if(childsCount==0){
            this.getEntityManager()
                    .createNativeQuery("update organization_structure set has_child=false where id=:id")
                    .setParameter("id", parentId.longValue())
                    .executeUpdate();
        }
    }
}
